package bera.lee.memorycard;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.BitmapFactory;

/**
 *<pre>
 * <b><font color="blue">Upload</font></b>
 * </pre>
 * 
 *<pre>
 * <b>&nbsp;--描述说明--</b>
 * </pre>
 * 
 * <pre></pre>
 * 
 * <pre>
 * <b>--样例--</b>
 *   Upload obj = new Upload();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * @author <b>beralee</b>
 */
public class Upload {
	/**
	 * 
	 * Hi my friend,don't use this code to test upload and get a High Score :-)  
	 * @param usernmame
	 * @param score
	 * @return
	 * @author:beralee
	 */
	public static String upload(String usernmame, String score) {
		String result = "error";
		
		  String url = "http://beraleeapp.appspot.com/worldscore?game=mc&username=" +
		  usernmame + "&score=" + score + "&key=bfirst";
		 URL myurl = null;
		try {
			myurl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return result;
		}
		InputStream is = null;
		try {
			HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(10000);
			conn.setRequestMethod("GET");
			conn.connect();
			is = conn.getInputStream();
			 BufferedReader   br=new   BufferedReader(new InputStreamReader(is));     
			  String   line   =   null;   
			  while((line   =   br.readLine())   !=   null){     
				  result=line;     
			  }

		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {

			}
		}
		return result;
	}
	public static String update(String usernmame, String score) {
		String result = "error";
		
		  String url = "http://beraleeapp.appspot.com/worldscoreupdate?game=mc&username=" +
		  usernmame + "&score=" + score + "&key=bfirst";
		 URL myurl = null;
		try {
			myurl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return result;
		}
		InputStream is = null;
		try {
			HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(10000);
			conn.setRequestMethod("GET");
			conn.connect();
			is = conn.getInputStream();
			 BufferedReader   br=new   BufferedReader(new InputStreamReader(is));     
			  String   line   =   null;   
			  while((line   =   br.readLine())   !=   null){     
				  result=line;     
			  }

		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {

			}
		}
		return result;
	}

	public static byte[] recieveByte(InputStream input) throws IOException {
		byte recieve[] = null;
		ByteArrayOutputStream barray = new ByteArrayOutputStream();
		DataInputStream dataInput = new DataInputStream(input);
		int ch;
		byte[] buffer = new byte[100];
		while ((ch = dataInput.read(buffer, 0, 100)) != -1) {
			barray.write(buffer, 0, ch);
		}
		recieve = barray.toByteArray();
		barray.close();
		return recieve;
	}
}
